<%@ page contentType="text/html; charset=UTF-8" language="java" isErrorPage="false" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>与${friendname}的聊天详情</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
    <link rel="Shortcut Icon" href="../../images/logo.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/css/bootstrap.css">
</head>
<body>
    <!-- 头部导航栏 -->
    <%@ include file="../header.jsp"%>
    <!-- 消息导航栏-->
    <section class="bg-white shadow-sm">
            <div class="container">
                <div class="nav-scroller bg-white">
                    <nav class="nav nav-underline">
                        <li class="nav-link text-muted" href="#">消息中心</li>
                        <a class="nav-link active" href="/message/list">私信</a>
                        <a class="nav-link" href="/message/send">
                            <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-mouse" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                                <path fill-rule="evenodd" d="M8 3a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 3zm4 8V5a4 4 0 0 0-8 0v6a4 4 0 0 0 8 0zM8 0a5 5 0 0 0-5 5v6a5 5 0 0 0 10 0V5a5 5 0 0 0-5-5z"/>
                            </svg>发送私信
                        </a>
                    </nav>
                </div>
        </div>
    </section>

    <!-- 聊天记录 -->
    <section>
        <div class="container">
            <div class="my-3 p-3 bg-white rounded shadow-sm">
                <h6 class="border-bottom border-gray pb-2 mb-0">
                    共有 ${msglist.size} 条与 <span class="text-primary">@${friendname}</span> 的聊天记录
                </h6>
                <c:forEach items="${msglist.getList()}" var="item">
                    <div class="media text-muted pt-3">
                        <a href="#"><img src="${item.photo}" class="img-fluid rounded-circle shadow-sm" style="width: 45px;"></a>
                        <p class="media-body pl-2 pb-3 mb-0 small lh-125 border-bottom border-gray">
                            <c:choose>
                                <c:when test="${item.toName == friendname}">
                                    <strong class="d-block text-gray-dark pb-1">您对 @${friendname} 说：
                                    <c:choose>
                                        <c:when test="${item.status == 0}">
                                            <span class="badge badge-info small">未读</span>
                                        </c:when>
                                        <c:otherwise>
                                            <span class="badge badge-secondary small">已读</span>
                                        </c:otherwise>
                                    </c:choose>
                                    </strong>
                                </c:when>
                                <c:otherwise>
                                    <strong class="d-block text-gray-dark pb-1">@${friendname} 对您说：
                                        <c:choose>
                                            <c:when test="${item.status == 1}">
                                                <span class="badge badge-info small">未读</span>
                                            </c:when>
                                            <c:otherwise>
                                                <span class="badge badge-secondary small">已读</span>
                                            </c:otherwise>
                                        </c:choose>
                                    </strong>
                                </c:otherwise>
                            </c:choose>
                                ${item.content}
                            <small class="d-block pt-1">${item.createTimeStr}</small>
                            <small class="d-block pt-1 text-right" style="margin-top: -25px">
                                <a href="/message/viewdetail?mid=${item.mid}" class="pl-2">删除</a>
                                <a href="#">举报</a>
                            </small>
                        </p>
                    </div>
                </c:forEach>
            </div>
            <nav aria-label="Page navigation example">
                <ul class="pagination">
                    <li class="page-item">
                        <a class="page-link" href="/message/viewdetail?friendname=${friendname}&?pageIndex=1" aria-label="Previous">
                            <span aria-hidden="true">首页</span>
                        </a>
                    </li>
                    <c:if test="${msglist.hasPreviousPage}">
                        <li class="page-item">
                            <a href="/message/viewdetail?friendname=${friendname}&pageIndex=${msglist.pageNum-1}" class="page-link" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    </c:if>
                    <li class="page-item">
                        <c:forEach items="${msglist.navigatepageNums}" var="page_Nums">
                        <c:if test="${page_Nums==msglist.pageNum}">
                    <li class="page-item active"><a href="#" class="page-link">${page_Nums}</a></li>
                    </c:if>
                    <c:if test="${page_Nums != msglist.pageNum}">
                        <li class="page-item"><a href="/message/viewdetail?friendname=${friendname}&pageIndex=${page_Nums}" class="page-link"></a></li>
                    </c:if>
                    </c:forEach>
                    </li>
                    <c:if test="${msglist.hasNextPage}">
                        <li class="page-item">
                            <a href="/message/viewdetail?friendname=${friendname}&pageIndex=${msglist.pageNum+1}" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    </c:if>
                    <li class="page-item">
                        <a href="/message/viewdetail?friendname=${friendname}&pageIndex=${msglist.pages}" class="page-link">末页</a>
                    </li>
                </ul>
            </nav>

        </div>
    </section>

    <!-- 消息发送框 -->
    <section class="mt-3">
       <div class="container">
           <div class="row">
                <div class="d-none d-lg-block col-md-1">
                    <img src="${photo}" class="rounded-circle img-fluid" style="max-width: 80px">
                </div>
                <div class="col-12 col-md-11 pl-5">
                    <form>
                        <div id="editor">
                            <div id="div1"></div>
                            <textarea class="form-control" id="inputContent" name="content" placeholder="content" hidden></textarea>
                        </div>
                        <button id="send" type="submit" class="btn btn-primary">发送</button> <span class="pl-2" id="result"></span>
                    </form>
                </div>
           </div>
       </div>
    </section>

    <!--引入JQuery,再引入popper.js，最后引入bootstrap.js-->
    <script src="/js/jquery.min.js"></script>
    <script src="/js/bootstrap.bundle.js"></script>
    <!-- 引入富文本编辑器.js -->
    <script type="text/javascript" src="/js/wangEditor.min.js"></script>
    <script type="text/javascript">
        const E = window.wangEditor
        const editor = new E('#div1')
        const $text1 = $('#inputContent')
        editor.config.focus = false
        editor.config.onchange = function (html) {
            // 第二步，监控变化，同步更新到 textarea
            $text1.val(html)
        }
        editor.create()

        // 第一步，初始化 textarea 的值
        $text1.val(editor.txt.html())
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#send").click(function () {
                var content = $('[name=content]').val();
                var messsage = {
                    friendName : "${friendname}",
                    content : content,
                };
                $.ajax({
                    type:"POST",
                    contentType:"application/json;charset=utf-8",
                    url:"/message/send",
                    data : JSON.stringify(messsage),
                    dataType:'JSON',
                    success:function (data) {
                        if (data.status){
                            $("#result").addClass("text-success");
                            $("#result").text("发送成功");
                            $("#InputContent").text("");
                            location.reload();
                        }else{
                            $("#result").addClass("text-danger");
                            $("#result").text(data.message);
                        }
                    },
                    error:function (e) {
                        alert("错误了");
                    }
                });
            });
        });
    </script>
    <script src="/js/message.js"></script>
</body>
</html>